package hot;

/**
 * @author way
 * @create 2025-03-15 17:20
 */
public class _33_search {
    public int search(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        // 寻找旋转点，即最小元素的位置
        while (l < r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] < nums[r]) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }

        if (target > nums[nums.length-1]) {
            l = 0;
            r = r - 1;
        } else {
            r = nums.length - 1;
        }

        // 在确定的范围内进行二分查找
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] > target) {
                r = mid - 1;
            } else if (nums[mid] < target) {
                l = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1};
        _33_search s = new _33_search();
        System.out.println(s.search(nums, 0));
    }
}
